﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FogBank : MonoBehaviour 
{

	public class FogData
	{
		public string		name = "";				
		public Color		fog_color = Color.white;
		public float		fog_density = 0.01f;
		public float		fog_start = 7.0f;
		public float		fog_end = 30.0f;
		public Color		ambient_color = Color.white;

	}


	public GameObject[]					FogPrefabArray = null;


	bool								mDefaultUseFog = false;
	float								mDefaultDensity = 0.01f;
	float								mDefaultLinearFogStart = 10.0f;
	float								mDefaultLinearFogEnd = 100.0f;
	Color								mDefaultFogColor = Color.white;
	Color								mDefaultAmbientColor = Color.white;
	List< FogData >						mFogDataList = new List< FogData >();

	// Use this for initialization
	void Start () 
	{
		if( this.FogPrefabArray == null )
		{
			this.enabled = false;
			return;
		}

		this.mDefaultUseFog = RenderSettings.fog;
		this.mDefaultDensity = RenderSettings.fogDensity;
		this.mDefaultLinearFogStart = RenderSettings.fogStartDistance;
		this.mDefaultLinearFogEnd = RenderSettings.fogEndDistance;
		this.mDefaultFogColor = RenderSettings.fogColor;
		this.mDefaultAmbientColor = RenderSettings.ambientLight;

		this.mFogDataList.Clear();
		for( int i = 0;		i < this.FogPrefabArray.Length;		++i )
		{
			GameObject go = GameObject.Instantiate( this.FogPrefabArray[ i ] ) as GameObject;
			FogModifier fm = go.GetComponent( typeof( FogModifier ) ) as FogModifier;
			FogData new_node = new FogData();
			new_node.ambient_color = fm.AmbientColor;
			new_node.fog_color = fm.FogColor;
			new_node.fog_density = fm.FogDensity;
			new_node.fog_start = fm.FogStart;
			new_node.fog_end = fm.FogEnd;
			new_node.name = go.name;
			this.mFogDataList.Add( new_node );
			GameObject.Destroy( go );
		}	
	}
	
	public void ChangeToDefault()
	{
		RenderSettings.fog = this.mDefaultUseFog;
		RenderSettings.fogDensity = this.mDefaultDensity;
		RenderSettings.fogStartDistance = this.mDefaultLinearFogStart;
		RenderSettings.fogEndDistance = this.mDefaultLinearFogEnd;
		RenderSettings.fogColor = this.mDefaultFogColor;
		RenderSettings.ambientLight = this.mDefaultAmbientColor;
	}

	void ChangeTo( int iIndex )
	{
		if( this.mFogDataList.Count == 0 )
			return;

		FogData fog_data = this.mFogDataList[ iIndex ];
		RenderSettings.fog = true;
		RenderSettings.fogColor = fog_data.fog_color;
		RenderSettings.fogStartDistance = fog_data.fog_start;
		RenderSettings.fogEndDistance = fog_data.fog_end;
		RenderSettings.fogDensity = fog_data.fog_density;
		RenderSettings.ambientLight = fog_data.ambient_color;
	}

	public void ChangeToDefeat()
	{
		int iIndex = -1;
		for( int i = 0;		i < this.mFogDataList.Count;		++i )
		{
			FogData cur_data = this.mFogDataList[ i ];
			if( cur_data.name.Contains( "Defeat" ) == true )
			{
				iIndex = i;
				break;
			}
		}
		if( iIndex != -1 )
		{
			this.ChangeTo( iIndex );
		}
	}
	public void ChangeToSpecialAttack()
	{
		int iIndex = -1;
		for ( int i = 0; i < this.mFogDataList.Count; ++i )
		{
			FogData cur_data = this.mFogDataList[ i ];
			if ( cur_data.name.Contains ( "Ultimate" ) == true )
			{
				iIndex = i;
				break;
			}
		}
		if ( iIndex != -1 )
		{
			this.ChangeTo ( iIndex );
		}
	}
	public void ChangeToVictory()
	{
		int iIndex = -1;
		for ( int i = 0; i < this.mFogDataList.Count; ++i )
		{
			FogData cur_data = this.mFogDataList[ i ];
			if ( cur_data.name.Contains ( "Victory" ) == true )
			{
				iIndex = i;
				break;
			}
		}
		if ( iIndex != -1 )
		{
			this.ChangeTo ( iIndex );
		}
	}


	
}
